<?php
namespace Home\Controller;
use Lib\Alidayu\SendSMS;
class TheorderController extends CommonController {

public function _initialize()
{
	// 临时创建订单用
}

//创建订单
public function order_add()
{
		$medialist = M("media")->field('id,media_name')->select();
		$leveldata = M("systemsetup")->where(array("type"=>1))->order('value desc')->select();
		$systemdata = M("systemsetup")->where(array("type"=>0))->select();
		// p($systemdata);exit();
		// $this->assign('labeldata',$labeldata);
		$this->assign('medialist',$medialist);
		$this->assign('leveldata',$leveldata);
		$this->assign('systemdata',$systemdata);
		$this->display();
}
public function media_ajax(){
	$media_id = I('get.media_id');
	$data['media'] = M("media")->field('mmid')->where("id=$media_id")->find();
	$mmid = $data['media']['mmid'];
	$data['special'] = M("mediaspecial")->field('id,specialname')->where("mediaid=$mmid")->select();
	exit(json_encode($data));
}
// 生成订单
public function add_order()
{
	$newsingle = I('post.newsingle');
	// p($_POST);exit();
	$oid = I('post.oid');
	$isdummy = I('post.isdummy');//是否为虚拟订单  1是
	$mid = I('post.media');//自媒体id
	$special = I('post.special');//专辑id
	$demand = I('post.demand');//订单类型
	$soundtype = I('post.soundtype');//声音类型
	$sex = I('post.sex');//性别
	$speed = I('post.speed');//订单类型
	$title = I('post.title');//标题
	$article = I('post.article');//文章
	$level = I('post.level');//主播等级id
	$wordnumber = I('post.wordnumber');//字数
	$expectedtime = I('post.expectedtime');//预计时间（分钟）
	$moneynumber = I('post.moneynumber');//原价
	$usemoney = I('post.usemoney');//实价
	$mintime = I('post.mintime');//每千字用时（秒）
	$delayed = I('post.delayed');//加时（秒）
	$perprice = I('post.perprice');//变价%
	$mediainfo = M("media")->where(array("id"=>$mid))->field('media_name,email')->find();
	$systemdata = M("systemsetup")->where("id=3")->where(array("type"=>0))->find();
	$wordstime = M("systemsetup")->where("id=22")->where(array("type"=>0))->find()['value'];
	$article = $this->removecss($article);
	$content = $this->PostOss($article);
	if (!$content) {
		$this->error('文章错误',"",1);
	}
	$theorder = D("theorder");
	if (!$theorder->create()){//自动验证
		$this->error('参数错误',"",1);
	}else{
		$now_time = time();
		// 是否为新手单 1 是 0 否
		$data['newsingle'] = $newsingle;
		// $data = $theorder->create(); // 保存生成的数据对象
		$data['isdummy'] = $isdummy;
		$data['mid'] = $mid;
		$data['mname'] = $mediainfo['media_name'];
		$data['email'] = $mediainfo['email'];
		$data['type'] = $soundtype;
		$data['speed'] = $speed;
		$data['createtime'] = $now_time;
		$data['expectedtime'] = $expectedtime*60-$delayed;
		$data['largestnumber'] = $systemdata['value'];
		$data['circulationtime'] = $delayed;
		$data['title'] = $title;
		$data['words'] = $wordnumber;
		$data['content'] = $content;
		$data['number'] = get_orderid();
		$data['state'] = 1;
		$data['attribute'] = 0;
		$data['demand'] = $demand;
		$data['money'] = $demand==3?$usemoney:$moneynumber;
		$data['premium'] = abs($usemoney-$moneynumber);
		$data['sex'] = $sex;
		$data['special'] = $special;
		$data['level'] = $level;
		$wordnumber = $wordnumber<100?100:$wordnumber;
		$data['wordstime'] = ceil($wordnumber/100*$wordstime/10);
		// 如果是新手单 流转为4
		$data['fail'] = $newsingle == 1 ? 4 : 3;
		// p($data);exit();
		if ($isdummy == 1) {
			$leveldata = M("systemsetup")->where(array("type"=>1))->select();
			$signsystem = M("systemsetup")->where(array("type"=>0))->select();
			foreach ($leveldata as $key => $value) {
				$data['level'] = $value['id'];
				$data['fail'] = 4;
				$data['money'] = ceil($wordnumber/100)*$value['money'];
				switch ($data['demand']) {
					case '2':$data['premium'] = $data['money']*$signsystem['8']['value']/100;break;
					case '3':$data['premium'] = $data['money']*$signsystem['13']['value']/100;$data['money']=$data['money']-$data['premium'];break;
					default:$data['premium'] = 0;break;
				}
		// p($data);exit();
				$result = $theorder->add($data);
				if ($result) {
					$data2['is_pay'] = 3;
					$data2['releasetime'] = time();
					$theorder->where(array('id'=>$result))->save($data2);//更新数据
				}
				// $res = $this->order_push($result,$signsystem);
			}
			$this->success('支付成功，已通知主播接单',"/Home/Order/theorder_list",1);
		}else{
			$signsystem = M("systemsetup")->where(array("type"=>0))->select();
			if ($signsystem['19']['value'] == 0) {
				$data['fail'] = 4;
			}
			$result = $theorder->add($data);
			$this->redirect('/Home/Order/edit_article', array('oid' => $result,'newsingle'=>$newsingle), 1, '页面跳转中...');
		}
		// p($data);exit();
	}

}

// 获取链接文章内容
function getwebcontent(){
	// $_POST['user_url'] = "https://mp.weixin.qq.com/s?__biz=MzIwMjUwNTcyMg==&tempkey=OTMxX2d5aFpCOGRZSk1rSFNVa1VZQlo4WXQySmkzR2xPSnc2Zy1SOE9lSHNvd0hTWXQtcjZ1VGhLRmtKa2ZVZFZ6eUQ2N3ptWWZlRFBMMzg4MGxVYWFCZEtDWGhCaWViR1B0dFlxX3BGQURqRXhWOTg0Uzd2eldLTUpEYjJPT2VJamxWZ2VVZjZ5SlBlTXBGeG9sLUY2TjU1WFgzUEU3WFR5VUJNM2pnRFF%2Bfg%3D%3D&chksm=16dced6121ab6477882ef589bd703da86d8f16b1cdc9ea6dbc29d43c277113eb17b3f595fd63&scene=0&previewkey=NdtaEQzKwauk7r4x%252F92AoMNS9bJajjJKzz%252F0By7ITJA%253D#rd";
	$url = $_POST['user_url'];
	$url = str_replace("https","http",$url);
	Vendor('phpQuery.phpQuery');
	$result = do_curl($url);
	$result = str_replace("<!--headTrap<body></body><head></head><html></html>-->", "", $result);
	\phpQuery::newDocumentHTML($result);
	if (strpos($url, "mp.weixin.qq.com") !== false) {
		$title = pq('title')->text();
		$str = pq('#js_content')->html();
	}elseif (strpos($url, "shimo.im") !== false) {
		$shimo =  file_get_contents($url);
		\phpQuery::newDocumentHTML($shimo);
		$title = pq('title')->text();
		$str = pq('script');
		$str = strip_tags(html_entity_decode($str));
		$str = strstr($str,"text");
		$str = explode('attribs', strval($str))['0'];
		$str = substr($str,10);
		$str = substr($str,0,-5);
		$str = str_replace("\\\\n","<br />",$str);
		$str = str_replace("*","",$str);
	}elseif (strpos($url, "www.jianshu.com") !== false) {
		$title = pq('title')->text();
		$str = pq('.show-content')->html();
	}
	$data['title'] = trim($title);
	$data['message'] = $this->removecss($str);
	exit(json_encode($data));
}

}